/*
 * 文件名： TbInfoActivity.java
 * 描    述：淘宝商品-WEB页面 
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.miguo.activity.base.BaseWebViewActivity;
import com.miguo.app.AppConfig;
import com.miguo.app.AppContext;
import com.miguo.bean.GoodsBean;
import com.miguo.framework.utils.Toast;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;
import com.miguo.utils.CommonUtils;
import com.miguo.utils.FavorUtils;
import com.miguo.view.listener.SingleClickEvent;

public class TbInfoActivity extends BaseWebViewActivity implements
        OnClickListener {

    private RelativeLayout favorBtn;
    private ProgressBar favorProgress;
    private GoodsBean goods;

    private ImageView likeImg;
    private String tbAppUrl;
    private ImageView priceClose;
    private RelativeLayout priceNoticeLy;
    private TextView priceNoticeText;
    private ImageView setNoti2;
    private ImageView tbappIcon;
    private ImageView unlikeLeft;
    private ImageView unlikeRight;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_taobao_info);
        initExtra();
        initWebConfig();
        initWebView();
        findViewById();
        initFavor();
        initTip();
        initTbAppBtn();
        mBaseHead.setTitle(goods.getTitle());
        mWebView.loadUrl(goods.getClick_url());
    }

    private void initExtra() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            goods = (GoodsBean) getIntent().getSerializableExtra("goods");
        } else {
            onBackPressed();
        }
    }

    private void findViewById() {
        favorBtn = (RelativeLayout) findViewById(R.id.web_favor);
        setNoti2 = (ImageView) findViewById(R.id.web_set_noti2);
        favorProgress = (ProgressBar) findViewById(R.id.web_progressBar);
        likeImg = (ImageView) findViewById(R.id.web_like);
        unlikeLeft = (ImageView) findViewById(R.id.web_unlike_left);
        unlikeRight = (ImageView) findViewById(R.id.web_unlike_right);
        tbappIcon = (ImageView) findViewById(R.id.web_gotb);
        priceNoticeLy = (RelativeLayout) findViewById(R.id.web_price_noticeLy);
        priceNoticeText = (TextView) findViewById(R.id.web_price_noticeText);
        priceClose = (ImageView) findViewById(R.id.web_price_noticeClose);
        priceClose.setOnClickListener(this);
        favorBtn.setOnClickListener(new SingleClickEvent(500L) {
            public void singleClick(View view) {
                FavorUtils.setFavorStatus(TbInfoActivity.this, goods, setNoti2,
                        favorProgress, likeImg, unlikeLeft, unlikeRight);
            }
        });
        tbappIcon.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.web_gotb:
                // 启动淘宝客户端
                toTAOBAOBtn();
                break;
            case R.id.web_price_noticeClose:
                // 关闭价格提示
                tipHide();
                break;
        }
        super.onClick(view);
    }


    private void initTip() {
        tipShow();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                tipHide();
            }
        }, 5000);
    }

    // 标签提示显示
    private void tipShow() {
        Animation animation = AnimationUtils.loadAnimation(TbInfoActivity.this,
                R.anim.tab_footer_appear);
        SpannableStringBuilder StringBuilder = new SpannableStringBuilder(
                "请忽略原价，拍下后自动变为" + goods.getCoupon_price() + "元");
        StringBuilder.setSpan(
                new AbsoluteSizeSpan(CommonUtils.dip2px(TbInfoActivity.this,
                        16.0F)), 13, 13 + goods.getCoupon_price().length(), 34);
        priceNoticeText.setText(StringBuilder);
        priceNoticeLy.setVisibility(View.VISIBLE);
        priceNoticeLy.startAnimation(animation);
    }

    // 标签提示隐藏
    private void tipHide() {
        Animation animation = AnimationUtils.loadAnimation(TbInfoActivity.this,
                R.anim.tab_footer_disappear);
        priceNoticeLy.setVisibility(View.GONE);
        priceNoticeLy.startAnimation(animation);
    }

    // private void setLayoutParams() {
    // RelativeLayout.LayoutParams webParam = (RelativeLayout.LayoutParams)
    // mViewFlipper
    // .getLayoutParams();
    // webParam.setMargins(0, 0, 0, CommonUtils.dip2px(mContext, 46.0f));
    // webParam.setMargins(0, CommonUtils.dip2px(mContext, 45.330002f), 0,
    // CommonUtils.dip2px(mContext, 46.0f));
    // mViewFlipper.setLayoutParams(webParam);
    // setToTBTextVisible(showJumpBtn());
    // }

    protected void setToTBTextVisible(boolean show) {
        if (show) {
            tbappIcon.setVisibility(View.VISIBLE);
            return;
        }
        tbappIcon.setVisibility(View.GONE);
    }

    /* 是否已收藏 */
    private void initFavor() {
        if (goods.getItem_id() > 1) {
            setNoti2.setVisibility(View.VISIBLE);
            if (!FavorUtils.isFavorite(TbInfoActivity.this, goods.getItem_id())) {
                setNoti2.setBackgroundResource(R.drawable.ic_favor_nor);
                return;
            }
            setNoti2.setBackgroundResource(R.drawable.ic_favor_sel);
            return;
        }
        setNoti2.setVisibility(View.GONE);
    }

    private void initTbAppBtn() {
        setToTBTextVisible(showJumpBtn());
    }

    protected void toTAOBAOBtn() {
        if (TextUtils.isEmpty(tbAppUrl) == false) {
            try {
                Uri uri = Uri.parse(tbAppUrl);
                Intent in = new Intent("android.intent.action.VIEW", uri);
                startActivity(in);
                Statistics.onTrackCustomClick(this,
                        AppConfig.STATISTICS.EVENT_NAME_29);
                return;
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
            }
        }
        Toast.show(mContext, "跳转失败，请刷新后重试");
    }

    private boolean showJumpBtn() {

        if ((AppContext.isTaobaoInstalled || AppContext.isTmallInstalled)
                && TextUtils.isEmpty(goods.getClick_url()) == false) {
            if (AppContext.isTaobaoInstalled) {
                tbAppUrl = goods.getClick_url().replaceFirst("http", "taobao");
            } else if (AppContext.isTmallInstalled) {
                tbAppUrl = goods.getClick_url().replaceFirst("http", "tmall");
            }
            return true;
        }
        return false;
    }


    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        String js = "var addScript = document.createElement(\"script\");";
        js += "addScript.src=\"http://js.meguo.com/app/js/app_android_v2.js\";";
        js += "document.body.appendChild(addScript);";
        mWebView.loadUrl("javascript:" + js);
    }

}
